package Com.Isman.Booking;

import java.util.ArrayList;
import java.util.List;

import org.kobjects.base64.Base64;

import Com.Isman.Adapter.HotelRoomAdapter;
import Com.Isman.WebService.NetworkCallRoomListener;
import Com.Isman.WebService.ServiceConnect;
import Com.Isman.WebService.ComplexType.Hotel;
import Com.Isman.WebService.ComplexType.Room;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

public class PageRoomList extends Activity implements NetworkCallRoomListener {
	class NetworkCallRoomListTask extends AsyncTask<String,Void,List<Room>>{
		private NetworkCallRoomListener listener = null;
		
		public NetworkCallRoomListTask(NetworkCallRoomListener listener) {
			this.listener = listener;
		}
		
		@Override
		protected List<Room> doInBackground(String... str) {
			ServiceConnect sc = new ServiceConnect();
			Log.v("Isman", "doInBackground RoomList");
			return sc.callGetRoomList(Integer.parseInt(str[0]),str[1],str[2]);
		}
		
		@Override
		protected void onPostExecute(List<Room> list) {
		   	listener.onNetworkCallRoomComplete(list);
		   	pbLoadRoom.setVisibility(View.INVISIBLE);
		}
		@Override
		protected void onPreExecute() {
		    super.onPreExecute();
		    pbLoadRoom.setVisibility(View.VISIBLE);
		}
		
	}
	
	public static final String  ROOMLIST_HOTEL = "Com.Isman.Booking.PageRoomList";
	private HotelRoomAdapter adapter;
	private Hotel hotel;
	private ImageView ivLogo;
	private ListView lvRoomList;
	private ProgressBar pbLoadRoom;
	
	
	private RatingBar rbRate;
	private List<Room> roomList = new ArrayList<Room>();
	private TextView tvCity;
	
	private TextView tvHotelName;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pageroomlist);
		hotel = (Hotel)getIntent().getSerializableExtra(ROOMLIST_HOTEL);
	
		
		tvHotelName	= (TextView)findViewById(R.id.lbl_HeadHotelName);
		tvCity		= (TextView)findViewById(R.id.lbl_HeadCity);
		lvRoomList = (ListView)findViewById(R.id.lvHotelRoomList);
		pbLoadRoom = (ProgressBar)findViewById(R.id.prbLoadRoomList);
		rbRate		= (RatingBar)findViewById(R.id.rtb_HeadRating);
		ivLogo		= (ImageView)findViewById(R.id.img_HeadLogo);
		
		
		
		tvHotelName.setText(hotel.getHotelName());
		tvCity.setText(hotel.getMap().getCity());
		rbRate.setRating(hotel.getHotelRating());
		
		
        byte[] decodedString = Base64.decode(hotel.getLogo().getContent());
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        ivLogo.setImageBitmap(decodedByte);
        
        
		adapter = new HotelRoomAdapter(this, roomList, hotel);
		lvRoomList.setAdapter(adapter);
		
		new NetworkCallRoomListTask(this).execute(new String[]{hotel.getHotelID()+"","2010-10-10","2012-10-10"});
	}
	@Override
	public void onNetworkCallRoomCancel() {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public void onNetworkCallRoomComplete(List<Room> list) {
		roomList.addAll(list);
		adapter.notifyDataSetChanged();
	}
}
